package offer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Creared with IntelliJ IDEA.
 * Description:一个整型数组里除了两个数字只出现一次，其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
 * User:yxd
 * Date:2022-08-11
 * Time:22:19
 */
public class BM52 {
    public int[] FindNumsAppearOnce (int[] array) {
        // write code here
        int[] nums = new int[2];
        Set<Integer> set = new HashSet<>();
        for(int i = 0;i < array.length; i++){
            if(set.contains(array[i])){
                set.remove(array[i]);
            }else{
                set.add(array[i]);
            }
        }
        int i = 0;
        for(int a : set){
            nums[i++] = a;
        }
        Arrays.sort(nums);
        return nums;
    }
    public int[] FindNumsAppearOnce1 (int[] array) {
        // write code here
        int[] nums = new int[2];
        Arrays.sort(array);
        int i = 0;
        int j = 0;
        while(i < array.length){
            while(i + 1 < array.length && array[i] == array[i + 1]){
                i += 2;
            }
            if(i < array.length){
                nums[j++] = array[i];
            }
            i++;
        }
        return nums;
    }
}
